package com.pys.beans;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.model.SelectItem;

import org.apache.commons.lang.StringUtils;
import org.primefaces.context.RequestContext;

import com.pys.common.beans.BaseBean;
import com.pys.pojo.PysKullanici;
import com.pys.pojo.PysProje;
import com.pys.pojo.PysTalep;
import com.pys.pojo.PysTalepOncelik;
import com.pys.pojo.PysTalepTip;
import com.pys.service.KullaniciService;
import com.pys.service.OncelikService;
import com.pys.service.ProjeService;
import com.pys.service.TalepService;
import com.pys.service.TalepTipService;
import com.pys.utils.FacesUtils;

@ManagedBean
@ViewScoped
public class TalepBean extends BaseBean implements Serializable {
	private List<SelectItem> projes;
	private Long projeId;
	private List<SelectItem> kullanicis;
	private Long atananKullaniciId;
	private List<SelectItem> talepTips;
	private Long talepTipId;
	private List<SelectItem> talepOnceliks;
	private Long talepOncelikId;
	private String ozet;
	private String tanim;
	private String dosya;
	private int isDegeri;
	private Date teslimTarih;
	private boolean durum = true;

	private PysTalep selectedTalep;
	private List<PysTalep> list;

	public TalepBean() {
		ProjeService projeService = new ProjeService();
		List<PysProje> pysProjes = projeService.getAllAktifList();
		projes = new ArrayList<SelectItem>();
		for (PysProje pysProje : pysProjes) {
			projes.add(new SelectItem(pysProje.getId(), pysProje.getAd()));
		}

		KullaniciService kullaniciService = new KullaniciService();
		List<PysKullanici> pysKullanicis = kullaniciService.getAllAktifList();
		kullanicis = new ArrayList<SelectItem>();
		for (PysKullanici pysKullanici : pysKullanicis) {
			kullanicis.add(new SelectItem(pysKullanici.getId(), pysKullanici
					.getAd() + " " + pysKullanici.getSoyad()));
		}

		TalepTipService talepTipService = new TalepTipService();
		List<PysTalepTip> pysTalepTips = talepTipService.getAllAktifList();
		talepTips = new ArrayList<SelectItem>();
		for (PysTalepTip pysTalepTip : pysTalepTips) {
			talepTips.add(new SelectItem(pysTalepTip.getId(), pysTalepTip
					.getAd()));
		}

		OncelikService oncelikService = new OncelikService();
		List<PysTalepOncelik> pysOnceliks = oncelikService.getAllAktifList();
		talepOnceliks = new ArrayList<SelectItem>();
		for (PysTalepOncelik pysOncelik : pysOnceliks) {
			talepOnceliks.add(new SelectItem(pysOncelik.getId(), pysOncelik
					.getAd()));
		}

		refreshList();
	}

	/*
	 * ACTIONS
	 */
	public void refreshList() {
		TalepService talepService = new TalepService();
		list = talepService.getAllList(false);
	}

	public void saveAction() {
		if (projeId == null || projeId == 0) {
			FacesUtils.addMessage(FacesMessage.SEVERITY_ERROR,
					"VALIDATION_PROJE");
			return;
		}
		if (StringUtils.isBlank(ozet)) {
			FacesUtils.addMessage(FacesMessage.SEVERITY_ERROR,
					"VALIDATION_OZET");
			return;
		}
		if (talepOncelikId == null || talepOncelikId == 0) {
			FacesUtils.addMessage(FacesMessage.SEVERITY_ERROR,
					"VALIDATION_TALEP_ONCELIK");
			return;
		}
		if (talepTipId == null || talepTipId == 0) {
			FacesUtils.addMessage(FacesMessage.SEVERITY_ERROR,
					"VALIDATION_TALEP_TIP");
			return;
		}
		if (atananKullaniciId == null || atananKullaniciId == 0) {
			FacesUtils.addMessage(FacesMessage.SEVERITY_ERROR,
					"VALIDATION_KULLANICI");
			return;
		}

		TalepService talepService = new TalepService();
		ProjeService projeService = new ProjeService();
		TalepTipService talepTipService = new TalepTipService();
		OncelikService oncelikService = new OncelikService();
		KullaniciService kullaniciService = new KullaniciService();
		if (selectedTalep == null) {
			PysTalep pysTalep = new PysTalep();
			pysTalep.setPysProje(projeService.getById(projeId));
			pysTalep.setPysTalepTip(talepTipService.getById(talepTipId));
			pysTalep.setPysTalepOncelik(oncelikService.getById(talepOncelikId));
			pysTalep.setPysKullaniciByOlusturanKullaniciId(FacesUtils
					.getUserSession().getCurrentUser());
			pysTalep.setPysKullaniciByAtananKullaniciId(kullaniciService
					.getById(atananKullaniciId));
			pysTalep.setOzet(ozet);
			pysTalep.setTanim(tanim);
			pysTalep.setIsDegeri(isDegeri);
			pysTalep.setTeslimTarih(teslimTarih);
			pysTalep.setDurum(durum);
			pysTalep.setOlusturmaTarih(new Date());
			pysTalep.setPysKullaniciByKullaniciId(FacesUtils.getUserSession()
					.getCurrentUser());

			talepService.save(pysTalep);
		} else {
			selectedTalep.setPysProje(projeService.getById(projeId));
			selectedTalep.setPysTalepTip(talepTipService.getById(talepTipId));
			selectedTalep.setPysTalepOncelik(oncelikService
					.getById(talepOncelikId));
			// selectedTalep.setPysKullaniciByOlusturanKullaniciId(FacesUtils
			// .getUserSession().getCurrentUser());
			selectedTalep.setPysKullaniciByAtananKullaniciId(kullaniciService
					.getById(atananKullaniciId));
			selectedTalep.setOzet(ozet);
			selectedTalep.setTanim(tanim);
			selectedTalep.setIsDegeri(isDegeri);
			selectedTalep.setTeslimTarih(teslimTarih);
			selectedTalep.setDurum(durum);
			selectedTalep.setGuncellemeTarih(new Date());
			selectedTalep.setPysKullaniciByKullaniciId(FacesUtils
					.getUserSession().getCurrentUser());
			talepService.update(selectedTalep);
		}
		refreshList();
		clear();
	}

	public void updateAction() {
		projeId = selectedTalep.getPysProje().getId();
		talepTipId = selectedTalep.getPysTalepTip().getId();
		talepOncelikId = selectedTalep.getPysTalepOncelik().getId();

		atananKullaniciId = selectedTalep.getPysKullaniciByAtananKullaniciId()
				.getId();
		ozet = selectedTalep.getOzet();
		tanim = selectedTalep.getTanim();
		isDegeri = selectedTalep.getIsDegeri();
		teslimTarih = selectedTalep.getTeslimTarih();
		durum = selectedTalep.isDurum();
	}

	public void deleteAction() {
		TalepService talepService = new TalepService();
		selectedTalep.setDeleted(true);
		talepService.update(selectedTalep);
		refreshList();
		clear();
	}

	public void clear() {
		selectedTalep = null;
		projeId = null;
		talepTipId = null;
		talepOncelikId = null;
		atananKullaniciId = null;
		ozet = null;
		tanim = null;
		isDegeri = 0;
		teslimTarih = null;
		durum = true;
	}

	public void closeAction() {
		RequestContext.getCurrentInstance().closeDialog(null);
	}

	/*
	 * GETTER-SETTER
	 */
	public List<SelectItem> getProjes() {
		return projes;
	}

	public void setProjes(List<SelectItem> projes) {
		this.projes = projes;
	}

	public Long getProjeId() {
		return projeId;
	}

	public void setProjeId(Long projeId) {
		this.projeId = projeId;
	}

	public List<SelectItem> getKullanicis() {
		return kullanicis;
	}

	public void setKullanicis(List<SelectItem> kullanicis) {
		this.kullanicis = kullanicis;
	}

	public Long getAtananKullaniciId() {
		return atananKullaniciId;
	}

	public void setAtananKullaniciId(Long atananKullaniciId) {
		this.atananKullaniciId = atananKullaniciId;
	}

	public List<SelectItem> getTalepTips() {
		return talepTips;
	}

	public void setTalepTips(List<SelectItem> talepTips) {
		this.talepTips = talepTips;
	}

	public Long getTalepTipId() {
		return talepTipId;
	}

	public void setTalepTipId(Long talepTipId) {
		this.talepTipId = talepTipId;
	}

	public List<SelectItem> getTalepOnceliks() {
		return talepOnceliks;
	}

	public void setTalepOnceliks(List<SelectItem> talepOnceliks) {
		this.talepOnceliks = talepOnceliks;
	}

	public Long getTalepOncelikId() {
		return talepOncelikId;
	}

	public void setTalepOncelikId(Long talepOncelikId) {
		this.talepOncelikId = talepOncelikId;
	}

	public String getDosya() {
		return dosya;
	}

	public void setDosya(String dosya) {
		this.dosya = dosya;
	}

	public int getIsDegeri() {
		return isDegeri;
	}

	public void setIsDegeri(int isDegeri) {
		this.isDegeri = isDegeri;
	}

	public PysTalep getSelectedTalep() {
		return selectedTalep;
	}

	public void setSelectedTalep(PysTalep selectedTalep) {
		this.selectedTalep = selectedTalep;
	}

	public List<PysTalep> getList() {
		return list;
	}

	public void setList(List<PysTalep> list) {
		this.list = list;
	}

	public String getTanim() {
		return tanim;
	}

	public void setTanim(String tanim) {
		this.tanim = tanim;
	}

	public String getOzet() {
		return ozet;
	}

	public void setOzet(String ozet) {
		this.ozet = ozet;
	}

	public Date getTeslimTarih() {
		return teslimTarih;
	}

	public void setTeslimTarih(Date teslimTarih) {
		this.teslimTarih = teslimTarih;
	}

	public boolean isDurum() {
		return durum;
	}

	public void setDurum(boolean durum) {
		this.durum = durum;
	}

}
